﻿package com.dykj.platform.iadapter.tags;

import java.io.IOException;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.struts2.components.Component;

import com.opensymphony.xwork2.util.ValueStack;

public class Page extends Component{
    private String curpage;  //当前页  
    private String totalPage;  //总页数  
    private String totalRecord;//总记录数

	public Page(ValueStack arg0) {
		super(arg0);
	}
	
	@Override
	public boolean start(Writer arg0) {
		boolean result = super.start(arg0);
		StringBuffer str = new StringBuffer();
		try {
            boolean isValid = true;  
            //从ValueStack中取出数值  
            if (isValid) { 
            	if(totalPage!=null){
            		if (totalPage.startsWith("%{") && totalPage.endsWith("}")) {  
            			totalPage = totalPage.substring(2, totalPage.length() -1);  
            			totalPage = String.valueOf(this.getStack().findValue(totalPage));  
            		} else {  
            			isValid = false;  
            		}
            	}else{
            		totalPage=String.valueOf(this.getStack().findValue("page.totalPageNum"));
            	}
            }  
            isValid = totalPage == null ? false : true;
            if (isValid) { 
            	if(curpage!=null){
            		if (curpage.startsWith("%{") && curpage.endsWith("}")) {  
            			curpage = curpage.substring(2, curpage.length() - 1);  
            			curpage = String.valueOf(this.getStack().findValue(curpage));  
            		} else {  
            			isValid = false;  
            		}  
            	}else{
            		curpage=String.valueOf(this.getStack().findValue("page.currentPageNo"));
            	}
            }  
            isValid = curpage == null ? false : true; 
            if (isValid) { 
            	if(totalRecord!=null){
            		if (totalRecord.startsWith("%{") && totalRecord.endsWith("}")) {  
            			totalRecord = totalRecord.substring(2, totalRecord.length() - 1);  
            			totalRecord = String.valueOf(this.getStack().findValue(totalRecord));  
            		} else {  
            			isValid = false;  
            		}  
            	}else{
            		totalRecord=String.valueOf(this.getStack().findValue("page.totalRecordNum"));
            	}
            }  
            isValid = totalRecord == null ? false : true; 
            if(isValid){
            	String pageMethod = String.valueOf(this.getStack().findValue("requestMsg['pageMethod']"));
            	if(pageMethod.startsWith("java.lang.Object")){
            		pageMethod = "queryForPage";
            	}
            	str.append("<div class='bottomPage'> 页：");
            	str.append(curpage);
            	str.append("/");
            	str.append(totalPage);
            	str.append("&nbsp;&nbsp;记录数：");
            	str.append(totalRecord);
            	str.append("&nbsp;&nbsp;&nbsp;&nbsp;[<a id='goToFirstPage'>首页</a>]&nbsp;&nbsp;[<a id='goToPreviousPage'>上一页</a>]&nbsp;&nbsp;[<a id='goToNextPage'>下一页</a>]&nbsp;&nbsp;[<a id='goToLastPage'>尾页</a>]");
            	if(curpage.equals("0")){
            		str.append("<input type='hidden' name='page.currentPageNo' value='1'");
            	}
            	str.append("  <select name='page.currentPageNo' id='curPageNo_dykj'>");
            	for(int i =1;i<=Integer.parseInt(totalPage);i++){
            		str.append(" <option value='");
            		str.append(i);
            		str.append("'");
            		if(Integer.parseInt(curpage)==i){
            			str.append("selected='selected'");
            		}
            		str.append(">");
            		str.append(i);
            		str.append("</option>");
            	}
            	str.append("</select>");
            	
            	str.append("<input type='hidden' id='curPage_hidden' value='");
            	str.append(curpage);
            	str.append("'>");
            	
            	str.append("<input type='hidden' id='allPage_hidden' value='");
            	str.append(totalPage);
            	str.append("'>");
            	
            	str.append("<script type='text/javascript'>");
        		str.append("$('#curPageNo_dykj').change(function(){");
        		str.append("	formSubmit('");
        		str.append(pageMethod);
        		str.append("');});");
            	if(!totalPage.equals("0")&&!curpage.equals("1")){
            		str.append("$('#goToFirstPage').attr('class','pageStyleClass').click(function(){");
            		str.append("	$('#curPageNo_dykj').val('1');");
            		str.append("	formSubmit('");
            		str.append(pageMethod);
            		str.append("');});");
                	str.append("$('#goToPreviousPage').attr('class','pageStyleClass').click(function(){");
                	str.append("	$('#curPageNo_dykj').val(parseInt($('#curPage_hidden').val())-1);");
            		str.append("	formSubmit('");
            		str.append(pageMethod);
            		str.append("');});");
            	}
            	if(!totalPage.equals("0")&&!totalPage.equals(curpage)){
            		str.append("$('#goToNextPage').attr('class','pageStyleClass').click(function(){");
            		str.append("	$('#curPageNo_dykj').val(parseInt($('#curPage_hidden').val())+1);");
            		str.append("	formSubmit('");
            		str.append(pageMethod);
            		str.append("');});");
            		str.append("$('#goToLastPage').attr('class','pageStyleClass').click(function(){");
            		str.append("	$('#curPageNo_dykj').val($('#allPage_hidden').val());");
            		str.append("	formSubmit('");
            		str.append(pageMethod);
            		str.append("');});");
            	}
        		str.append("</script></div>");
            }
			arg0.write(str.toString());
		} catch (IOException e) {
			Logger.getLogger(Input.class.getName()).log(Level.SEVERE, null, e);
		}
		return result;
	}

	public String getCurpage() {
		return curpage;
	}

	public void setCurpage(String curpage) {
		this.curpage = curpage;
	}

	public String getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(String totalPage) {
		this.totalPage = totalPage;
	}

	public String getTotalRecord() {
		return totalRecord;
	}

	public void setTotalRecord(String totalRecord) {
		this.totalRecord = totalRecord;
	}
	
}
